package com.qingke.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import org.omg.CORBA.portable.InputStream;

public class Dao extends HttpServlet{

	public Dao() {
		
	}

	public  Connection getConnection(){
		Connection conn=null;
//		String driver="com.mysql.jdbc.Driver";
//		String url = "jdbc:mysql://localhost:3306/student_manage";
//		String u = "root";
//		String p = "root";
		String driver=getProperties("db.driver");
		String url = getProperties("db.url");
		String u = getProperties("db.username");
		String p = getProperties("db.password");
		
		try {
			Class.forName(driver);
			conn =DriverManager.getConnection(url, u, p);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	private String getProperties(String key) {
		Properties props = new Properties();
		try {
			String path="C:\\Users\\qingke04\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\LoginTest\\jdbc.properties";
			//InputStream in= (InputStream) getServletContext().getResourceAsStream("/jdbc.properties");
			//FileInputStream in= new FileInputStream(path);
			//String path=getServletContext().getRealPath("/jdbc.properties");
			//System.out.println(path);
			FileInputStream in= new FileInputStream(path);
			props.load(in);
			return props.getProperty(key);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

}
